package cn.iocoder.boot.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import cn.iocoder.boot.dto.ComponentPageDTO;
import cn.iocoder.boot.dto.gc.TemplateFields;
import cn.iocoder.boot.service.GcService;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class GcServiceImpl implements GcService {

    private static String localGcUrl = "http://192.168.10.185";
    private static String gcLocalGcUrl = "http://192.168.20.50:9037";
//    private static String gcLocalGcUrl = "http://devapi1.eeidm.com:9037";
    private static String localPassword = "123456";
    private static String localName = "chenzx@kingbrother.com";
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String getToken() {
            //先从缓存拿
        String localGcToken = stringRedisTemplate.opsForValue().get("localGcToken");
            if (StringUtils.isNotBlank(localGcToken)){
                return localGcToken;
            }
            // 定义请求参数
            String url = localGcUrl+"/to/api/base/system/auth/login";
            // 构建请求参数
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("userType", 2);
            paramMap.put("password", localPassword);
            paramMap.put("email", localName);
            try {
                // 发送带有自定义头部参数的 GET 请求并获取响应
                // 发送 GET 请求
                HttpResponse response = HttpRequest.post(url)
                        .body(JSONUtil.toJsonStr(paramMap))
                        .execute();
                String accessToken = null;
                try {
                    JSONObject jsonObject = JSONObject.parseObject(response.body());
                    accessToken = jsonObject.getJSONObject("data").getString("accessToken");
                } catch (Exception e) {
                    throw new RuntimeException(response.body());
                }
                //存到缓存
                stringRedisTemplate.opsForValue().set("localGcToken", accessToken, 60 * 60 * 5  , java.util.concurrent.TimeUnit.SECONDS);
                return accessToken;
            } catch (Exception e) {
                throw new RuntimeException(e.getMessage());
        }

    }

    @Override
    public boolean createComponents(ComponentPageDTO componentPageDTO) {
        //todo 查询是否已存在

        TemplateFields templateFields = new TemplateFields();
        templateFields.setKey0(componentPageDTO.getModel());
        templateFields.setKey1(componentPageDTO.getBrand());
        templateFields.setKey7(componentPageDTO.getSpecifications());
        templateFields.setKey35("kb-components");

        // 定义请求参数
        String url = gcLocalGcUrl+"/business/createComponents";
        try {
            HttpResponse response = HttpRequest.post(url)
                    .body(JSONUtil.toJsonStr(templateFields))
                    .execute();
                JSONObject jsonObject = JSONObject.parseObject(response.body());
            int status = jsonObject.getIntValue("status");
            if (status!=200){
                throw new RuntimeException("新增失败");
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
        return true;
    }
}
